load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("//bazel/toolchains/cc/mongo_linux:mongo_toolchain.bzl", "setup_mongo_toolchains")
load("//bazel/toolchains/cc/mongo_linux:mongo_gdb.bzl", "setup_gdb_toolchains")

http_archive(
    name = "aspect_rules_lint",
    sha256 = "f60e4a737a5e09402f5fa3bd182efa80dac5523ca4b9bc5c6fa8c06fbfb46630",
    strip_prefix = "rules_lint-1.1.0",
    url = "https://github.com/aspect-build/rules_lint/releases/download/v1.1.0/rules_lint-v1.1.0.tar.gz",
)

setup_mongo_toolchains()

setup_gdb_toolchains()

http_archive(
    name = "windows_sasl",
    build_file_content = """
package(default_visibility = ["//visibility:public"])

filegroup(
    name = "includes",
    srcs = select({
        "@platforms//os:windows": glob(["include/**/*.h"]),
        "//conditions:default": [],
    }),
)

filegroup(
    name = "libraries",
    srcs = select({
        "@platforms//os:windows": glob(["lib/**/*"]),
        "//conditions:default": [],
    }),
)

filegroup(
    name = "bins",
    srcs = select({
        "@platforms//os:windows": glob(["bin/**/*"]),
        "//conditions:default": [],
    }),
)
    """,
    sha256 = "3e22e2b16f802277123590f64dfda44f1c9c8a2b7e758180cd956d8ab0965817",
    urls = [
        "https://s3.amazonaws.com/boxes.10gen.com/build/windows_cyrus_sasl-2.1.28.zip",
    ] * 5,
)

http_archive(
    name = "wix_toolset",
    build_file_content = """
package(default_visibility = ["//visibility:public"])

filegroup(
    name = "wix_binaries",
    srcs = select({
        "@platforms//os:windows": glob(["*"]),
        "//conditions:default": [],
    }),
)

filegroup(
    name = "candle",
    srcs = select({
        "@platforms//os:windows": ["candle.exe"],
        "//conditions:default": [],
    }),
    data = select({
        "@platforms//os:windows": [":wix_binaries"],
        "//conditions:default": [],
    }),
)

filegroup(
    name = "light",
    srcs = select({
        "@platforms//os:windows": ["light.exe"],
        "//conditions:default": [],
    }),
    data = select({
        "@platforms//os:windows": [":wix_binaries"],
        "//conditions:default": [],
    }),
)
    """,
    sha256 = "6ac824e1642d6f7277d0ed7ea09411a508f6116ba6fae0aa5f2c7daa2ff43d31",
    urls = [
        "https://github.com/wixtoolset/wix3/releases/download/wix3141rtm/wix314-binaries.zip",
    ] * 5,
)

load("//bazel/install_rules:windows_msvc.bzl", "windows_msvc")

windows_msvc(
    name = "local_windows_msvc",
)

load("//bazel/coverity:coverity_toolchain.bzl", "coverity_toolchain")

coverity_toolchain(
    name = "rules_coverity",
)

load("@rules_coverity//coverity:repositories.bzl", "rules_coverity_toolchains")

rules_coverity_toolchains()

load("@rules_poetry//rules_poetry:poetry.bzl", "poetry")

poetry(
    name = "poetry",
    excludes = [
        "mdit-py-plugins",  # plugins for markdown-it-py. Introduces an optional circular dependency that bazel does not like.
    ],
    lockfile = "//:poetry.lock",
    pyproject = "//:pyproject.toml",
    python_interpreter_target_default = "@py_host//:dist/bin/python3",
    python_interpreter_target_mac = "@py_host//:dist/bin/python3",
    python_interpreter_target_win = "@py_host//:dist/python.exe",
)

load("@aspect_rules_js//js:toolchains.bzl", "DEFAULT_NODE_VERSION", "rules_js_register_toolchains")

rules_js_register_toolchains(node_version = DEFAULT_NODE_VERSION)

load("@aspect_rules_js//npm:repositories.bzl", "npm_translate_lock")

npm_translate_lock(
    name = "npm",
    npmrc = "//:.npmrc",
    pnpm_lock = "//:pnpm-lock.yaml",
    verify_node_modules_ignored = "//:.bazelignore",
)

load("@npm//:repositories.bzl", "npm_repositories")

npm_repositories()

# Sub in the system openssl for boringssl since we don't want two implementations of
# ssl in the same address space.
new_local_repository(
    name = "boringssl",
    build_file_content = """
cc_library(
  name = "crypto",
  linkopts = ["-lcrypto"],
  visibility = ["//visibility:public"],
)
cc_library(
  name = "ssl",
  linkopts = ["-lssl"],
  visibility = ["//visibility:public"],
)
""",
    path = "bazel/_openssl_placeholder_for_grpc",
)

# Overloads for the vendored repositories.
#
# WARNING: Don't change the order of the deps() calls and local_repositories.
#          They're read linearly dependencies that come first override later
#          ones. Dependency updates might change the correct order, though it's
#          unlikely. This is obviously a temporary solution and will no longer
#          be necessary once migration to bzlmod is complete.

# Note: rules_python is implicitly loaded with a grpc-compatible version.

load("//bazel/install_rules:pigz.bzl", "setup_pigz")

setup_pigz(
    name = "pigz",
)

load("//bazel/install_rules:zstd.bzl", "setup_zstd")

setup_zstd(
    name = "zstd",
)

# This repository is normally created by db-contrib-tool or manually extracting the binaries at the proper location
new_local_repository(
    name = "mongot_localdev",
    build_file_content = """
package(default_visibility = ["//visibility:public"])
filegroup(
    name = "mongot_binaries",
    srcs = glob(["**"], exclude = ["BUILD.bazel", "WORKSPACE", "MODULE.bazel", "MODULE.bazel.lock"]),
)
""",
    path = "mongot-localdev",
)

local_repository(
    name = "bazel_rules_mongo",
    path = "buildscripts/bazel_rules_mongo",
    repo_mapping = {"@poetry": "@poetry_bazel_rules_mongo"},
)

load("@bazel_rules_mongo//codeowners:codeowners_validator.bzl", "codeowners_validator")

codeowners_validator()

load("@bazel_rules_mongo//codeowners:codeowners_binary.bzl", "codeowners_binary")

codeowners_binary()

poetry(
    name = "poetry_bazel_rules_mongo",
    lockfile = "@bazel_rules_mongo//:poetry.lock",
    pyproject = "@bazel_rules_mongo//:pyproject.toml",
    python_interpreter_target_default = "@py_host//:dist/bin/python3",
    python_interpreter_target_mac = "@py_host//:dist/bin/python3",
    python_interpreter_target_win = "@py_host//:dist/python.exe",
)

load("//bazel/format:shfmt.bzl", "shfmt")

shfmt()
